﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using ProductIntory.Domains;
using ProductIntory.ErrorCodes;
using ProductIntory.Infrastructures.Interface;
using ProductIntory.Read.Apis.Application.Command.ProductInspectionCommands;

namespace ProductIntory.Read.Apis.Application.CommandHandler.ProductInspectionCommandHandlers
{
    public class UpdateProductInspectionCommandHandler : IRequestHandler<UpdateProductInspectionCommand, ApiResult<int>>
    {
        IBaseRepository<ProductInspection> ProductInspection;

        public UpdateProductInspectionCommandHandler(IBaseRepository<ProductInspection> ProductInspection)
        {
            this.ProductInspection = ProductInspection;
        }

        public async Task<ApiResult<int>> Handle(UpdateProductInspectionCommand request, CancellationToken cancellationToken)
        {
            var list =await ProductInspection.GetAll().FirstOrDefaultAsync(c=>c.ProductsInspection_Id==request.ProductInspection.ProductsInspection_Id);
            await ProductInspection.Update(request.ProductInspection);

            return new ApiResult<int> 
            {
                Code=ApiEnum.success,
                Message="Product Inspection updated successfully",
                Data=1
            };
        }
    }
}
